package com.pro.xwt.rest.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.xwt.common.model.BEmployee;
import com.pro.xwt.common.model.BTask;
import com.pro.xwt.common.model.RProjFlow;
import com.pro.xwt.common.service.BProjectService;
import com.pro.xwt.common.service.BTaskService;
import com.pro.xwt.common.service.RProjFlowService;
import com.pro.xwt.common.utils.Common;
import com.pro.xwt.rest.ResponseResult;

@Controller
@RequestMapping("/projFlows")
public class RProjFlowController {
	@Autowired
	RProjFlowService rProjFlowService;
	@Autowired
	BProjectService bProjectService;
	@Autowired
	BTaskService bTaskService;

	/**
	 * 添加项目流程
	 * 
	 * @param rProjEmp
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.POST }, value = "")
	@ResponseBody
	public ResponseResult add(@RequestBody RProjFlow rProjFlow, HttpServletRequest request) {
		if (!isOwnerOrAdmin((BEmployee) request.getAttribute("user"), rProjFlow.getProjId())) {
			return ResponseResult.fail("你没有该权限", false);
		}
		BEmployee user= (BEmployee) request.getAttribute("user");
		Date date = new Date();
		rProjFlow.setId(Common.generateUUID());
		rProjFlow.setCreateTime(date);
		rProjFlow.setCreateEmpId(user.getId());
		Integer flag = rProjFlowService.insert(rProjFlow);
		if (flag <= 0) {
			return  ResponseResult.fail("插入失败", null);
		}
		return ResponseResult.success("添加资源成功", rProjFlow);
	}
	
	/**
	 * 修改任务
	 * @param taskId
	 * @param bTask
	 * @param request
	 * @return
	 */
	@RequestMapping(method = { RequestMethod.PATCH }, value = "/tasks/{taskId}")
	@ResponseBody
	public ResponseResult update(@PathVariable String taskId, @RequestBody BTask bTask,
			HttpServletRequest request) {
		    bTask.setId(taskId);
		    int flag = bTaskService.update(bTask);
		    if(flag==1){
		    	BTask  bT=bTaskService.findById(taskId);
		    	return ResponseResult.success("任务修改成功", bT);
		    }
		return ResponseResult.success("任务修改失败", flag);
	}
	/**
	 * 删除流程
	 * 
	 * @param bEmployee
	 * @return
	 * 
	 */
	@RequestMapping(method = { RequestMethod.DELETE }, value = "{id}/projects/{projId}")
	@ResponseBody
	public ResponseResult deleteFlow(@PathVariable String id, @PathVariable String projId, HttpServletRequest request) {
		if (!isOwnerOrAdmin((BEmployee) request.getAttribute("user"), projId)) {
			return ResponseResult.fail("你没有该权限", false);
		}
		Integer flag = rProjFlowService.deleteFlowById(id);
		if (flag > 0) {
			return ResponseResult.success("删除流程成功", flag);
		}
		return ResponseResult.fail("删除资源失败", flag);
	}
	
	
	/**
	 * 修改流程名
	 * 
	 * @param bEmployee
	 * @return
	 * 
	 */
	@RequestMapping(method = { RequestMethod.PATCH }, value = "{id}/projects/{projId}")
	@ResponseBody
	public ResponseResult updateFlow(@PathVariable String id, @PathVariable String projId, HttpServletRequest request,@RequestBody RProjFlow rProjFlow) {
		if (!isOwnerOrAdmin((BEmployee) request.getAttribute("user"), projId)) {
			return ResponseResult.fail("你没有该权限", false);
		}
		rProjFlow.setId(id);
		Integer flag = rProjFlowService.update(rProjFlow);
		if (flag > 0) {
			return ResponseResult.success("修改流程名成功", rProjFlow);
		}
		return ResponseResult.fail("修改流程名失败失败", flag);
	}
	
	public boolean isOwnerOrAdmin(BEmployee bEmployee, String projId) {
		if (bEmployee.getRole().equals("ADMIN")) {
			return true;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("createEmpId", bEmployee.getId());
		map.put("id", projId);
		bProjectService.findByCondition(map);
		return (bProjectService.findByCondition(map) != null);
	}
}
